function ModuleAdmin() {
	log( "[ModuleAdmin]" );
}

ModuleAdmin.prototype.initialize = function() {
	log( "[ModuleAdmin.initialize]" );

	bindEvents();
	retrieveUsers();
	viewPortStatusList();

	refreshUserList( function() {
		var image = $("input[type=image]#refreshUserList");
		toogleBusy( image );
		retrieveUsers();
	}, 30000); //30 seconds
}

function bindEvents() {
	log( "[ModuleAdmin.bind]" );

	$("input[type=image]#refreshUserList").live( 'click', function( event ) {
		toogleBusy( event.currentTarget );
		retrieveUsers();
	});

	$("input[type=image]#refreshPortStatusList").live( 'click', function( event ) {
		toogleBusy( event.currentTarget );
		viewPortStatusList();
	});

	$("input[type=image]#resetPortStatus").live( 'click', function( event ) {
		toogleBusy( event.currentTarget );
		resetPortStatus();
	});

	$("input[type=image]#calendar").live( 'click', function( event ) {
		viewUserInfo( event );
	});

	$("input[type=image]#closeTimeRegisterList").live( 'click', function() {
		hideTimeRegisterList();
	});

	$("input[type=image]#message").live( 'click', function( event ) {
		viewUserMessageForm( event );
	});

	$("input[type=image]#closeMessageForm").live( 'click', function() {
		hideMessageForm();
	});

	$("input#sendMessage").live( 'click', function( event ) {
		sendMessage( event );
	});

//	$("input#messageText").live( 'focusin', function() {
//		$("input#messageText").removeClass( "empty" );
//	});
//
//	$("input#messageText").live( 'focusout', function() {
//		if( $("input#messageText").val() == "" ) {
//			$("input#messageText").addClass( "empty" );
//		}
//	});

	$("input[type=checkbox]").live( 'change', function( event ) {
		updatePortStatus( event );
	});

	hideTimeRegisterList();
	hideMessageForm();
}

function retrieveUsers() {
	log( "[ModuleAdmin.retrieveUsers]" );

	$.getJSON( "/admincasa/rest/users", function( data ) {

		if( hasCompleteData( data ) ) {

			$("#userList").html("");

			//head
			$("<tr class='title'>" +
				"<td>User <input type='image' id='refreshUserList' src='/admincasa/images/refresh.png' title='refresh' /></td>" +
				"<td>Computer Name</td>" +
				"<td>Last Date</td>" +
					"</tr>").appendTo("#userList");

			//body
			$.each( data.data, function( i, item ) {
				$("<tr class='row'>" +
					"<td><img src='/admincasa/images/" + item.status + ".png'/>" +
							item.userName +
							"<input type='image' id='message' src='/admincasa/images/mail.png' metadata='" + item.userName + "'/>" +
							"<input type='image' id='calendar' src='/admincasa/images/calendar.png' metadata='" + item.userName + "'/></td>" +
					"<td>" + item.computerName + "</td>" +
					"<td>" + item.startDate + "</td>" +
						"</tr>").appendTo("#userList");

			});

			$("#userList").show();
			$("#timeRegisterList").html("").attr('user', "");
			hideTimeRegisterList();
			hideMessageForm();

		}

	}).error( function( data ) {
		showErrorMessage( data.statusText );
	});
}

function viewUserInfo( event ) {
	log( "[ModuleAdmin.viewUserInfo]" );

	var user = $(event.currentTarget).attr("metadata");
	var tableUser = $("#timeRegisterList").attr('user');

	if( tableUser == undefined || tableUser != user ) {

		$.getJSON( "/admincasa/rest/" + user, function( data ) {

			if( hasCompleteData( data ) ) {

				$("#timeRegisterList").html("").attr('user', user);

				//head
				$("<tr class='title'>" +
					"<td>Computer Name</td>" +
					"<td>Start Date</td>" +
					"<td>End Date <input type='image' id='closeTimeRegisterList' src='/admincasa/images/delete.png' title='close'/></td>" +
						"</tr>").appendTo("#timeRegisterList");

				//body
				$.each( data.data, function( i, item ) {
					$("<tr class='row'>" +
						"<td>" + item.computerName + "</td>" +
						"<td>" + item.startDate + "</td>" +
						"<td>" + item.endDate + "</td>" +
							"</tr>").appendTo("#timeRegisterList");

				});

				$("#timeRegisterList").show();
				$("#timeRegisterList").parent().show();

			}

		}).error( function( data ) {
			showErrorMessage( data.statusText );
		});
	} else {

		if( $("#timeRegisterList").is(":hidden") ) {
			showTimeRegisterList();
		} else {
			hideTimeRegisterList();
		}
	}
}

function viewUserMessageForm( event ) {
	log( "[ModuleAdmin.viewUserMessageForm]" );

	var user = $(event.currentTarget).attr("metadata");
	var tableUser = $("#messageForm").attr('user');

	if( tableUser == undefined || tableUser != user ) {

		$("#messageForm").html("").attr('user', user);

		//head
		$("<tr class='title'>" +
			"<td>New message to: " + user +
					"<input type='image' id='closeMessageForm' src='/admincasa/images/delete.png' title='close'/></td>" +
				"</tr>").appendTo("#messageForm");

		//body
		$("<tr class='row'>" +
			"<td>Message: <input id='messageText' type='text' placeholder='Message' required='required' />" +
					"<input id='sendMessage' type='image' src='/admincasa/images/forward.png' alt='Send' title='send message' metadata='" + user + "' /></td>" +
				"</tr>").appendTo("#messageForm");

		showMessageForm();
	} else {

		if( $("#messageForm").is(":hidden") ) {
			showMessageForm();
		} else {
			hideMessageForm();
		}
	}
}

function sendMessage( event ) {
	log( "[ModuleAdmin.sendMessage]" );

	var user = $(event.currentTarget).attr("metadata");

	if( $("#messageText").val() == "" ) {
		$("#messageForm").effect("bounce", { times:3, distance:'5' }, 300);
	} else {

		var messageText = $("#messageText").val();

		$.getJSON( "/admincasa/rest/message/" + user + "/" + messageText, function( data ) {

			if( hasCompleteData( data ) ) {
				if( data.data.new == false ) {
					$("#messageText").val( "" );
					hideMessageForm();
				}
			}

		}).error( function( data ) {
			showErrorMessage( data.statusText );
		});
	}
}

function viewPortStatusList() {
	log( "[ModuleAdmin.viewPortStatusList]" );

	$.getJSON( "/admincasa/rest/pp", function( data ) {

		if( hasCompleteData( data ) ) {

			$("#portsList").html("");

			//head
			$("<tr class='title'>" +
				"<td>Description <input type='image' id='refreshPortStatusList' src='/admincasa/images/refresh.png' title='refresh' /></td>" +
				"<td>State <input type='image' id='resetPortStatus' src='/admincasa/images/back.png' title='reset' /></td>" +
					"</tr>").appendTo("#portsList");

			//body
			$.each( data.data, function( i, item ) {

				$("<tr class='row'>" +
					"<td>" + item.description + "</td>" +
					"<td><input id='" + item.id + "' type='checkbox'> activated</input></td>" +
						"</tr>").appendTo("#portsList");

				if( item.activated ) {
					$("input[type=checkbox]#" + item.id ).attr('checked', true);
				} else {
					$("input[type=checkbox]#" + item.id ).attr('checked', false);
				}

			});

		}

	}).error( function( data ) {
		showErrorMessage( data.statusText );
	});
}

function resetPortStatus() {
	log( "[ModuleAdmin.resetPortStatus]" );

	$.getJSON( "/admincasa/rest/pp/reset", function( data ) {

		if( hasCompleteData( data ) ) {
			viewPortStatusList();
		}

	}).error( function( data ) {
		showErrorMessage( data.statusText );
	});
}

function updatePortStatus( event ) {
	log( "[ModuleAdmin.updatePortStatus]" );

	var portNumber = $(event.currentTarget).attr("id");
	var activated  = $(event.currentTarget).is(":checked");
	var disabled = $(event.currentTarget).attr("disabled");

	if( !disabled ) {

		$("input[type=checkbox]").attr('disabled', 'disabled');

		$.getJSON( "/admincasa/rest/pp/" + portNumber + "/" + activated, function( data ) {

			if( hasCompleteData( data ) ) {
				$("input[type=checkbox]").removeAttr('disabled');
			}

		}).error( function( data ) {
			$("input[type=checkbox]").removeAttr('disabled');
			$("input[type=checkbox]#" + portNumber ).attr('checked', !activated);
			showErrorMessage( data.statusText );
		});
	}
}

function toogleBusy( image ) {
	log( "[ModuleAdmin.toogleBusy]" );

	$(image).attr( 'src', '/admincasa/images/spinner.gif' );
}

function hideTimeRegisterList() {
	log( "[ModuleAdmin.hideTimeRegisterList]" );

	$("#timeRegisterList").hide();
	$("#timeRegisterList").parent().hide();
}

function showTimeRegisterList() {
	log( "[ModuleAdmin.showTimeRegisterList]" );

	$("#timeRegisterList").show();
	$("#timeRegisterList").parent().show();
}

function hideMessageForm() {
	log( "[ModuleAdmin.hideMessageForm]" );

	$("#messageForm").hide();
	$("#messageForm").parent().hide();
}

function showMessageForm() {
	log( "[ModuleAdmin.showMessageForm]" );

	$("#messageForm").show();
	$("#messageForm").parent().show();
}

function hasCompleteData( data ) {
	log( "[ModuleAdmin.hasCompleteData]" );

	if( data.errorMsg != null ) {
		showErrorMessage( data.errorMsg )
		return false
	}
	return data.data != null;
}

function showErrorMessage( message ) {
	log( "[ModuleAdmin.showErrorMessage]" );
	log( message );
	$("#error").text( message ).slideDown( 900 ).delay( 2400 ).slideUp( 900 );
}

function log( message ) {
	if( debugActivated && message != null ) {
		console.log( message );
	}
}

var debugActivated = true;

var moduleAdmin = new ModuleAdmin();
